package net.qhzw.geologr5.gis.service.system.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.qhzw.geologr5.gis.common.framework.utils.JsonResult;
import net.qhzw.geologr5.gis.service.system.entity.DictSoftnail;
import net.qhzw.geologr5.gis.service.system.mapper.DictSoftnailMapper;
import net.qhzw.geologr5.gis.service.system.service.DictSoftnailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;


@Service
@Slf4j
public class DictSoftnailServiceImpl implements DictSoftnailService {

    @Autowired
    private DictSoftnailMapper dictSoftnailMapper;


    /****
     * 查询软件列表
     * @return
     */
    @Override
    public JsonResult getSoftnail() {
        try {
            List<DictSoftnail> selectList = dictSoftnailMapper.selectList(null);
            log.info("selectList: {}", selectList);
            return JsonResult.success(selectList);
        } catch (Exception e) {
            log.error("查询软件列表失败==DictSoftnailServiceImpl==method:selectList==errMsg: {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }

    }


    /***
     * 新增编辑软件表
     * @param dictSoftnail
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult edit(DictSoftnail dictSoftnail) {
        try {
            if (dictSoftnail.getId() != null && dictSoftnail.getId() > 0) {
                int count = dictSoftnailMapper.updateById(dictSoftnail);
                if (count > 0) {
                    return JsonResult.success();
                }
                return JsonResult.error("修改失败");
            }
            int count = dictSoftnailMapper.insert(dictSoftnail);
            if (count > 0) {
                return JsonResult.success();
            }
            return JsonResult.error("添加失败");
        } catch (Exception e) {
            log.error("编辑软件列表失败==DictSoftnailServiceImpl==method:edit==errMsg: {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }
    }


    /***
     * 根据id删除软件表
     * @param ids
     * @return
     */
    @Override
    public JsonResult delete(Integer[] ids) {
        try {
            List<Integer> list = Arrays.asList(ids);
            int count = dictSoftnailMapper.deleteBatchIds(list);
            if (count > 0) {
                return JsonResult.success();
            }
            return JsonResult.error("添加失败");
        } catch (Exception e) {
            log.error("删除软件表失败==DictSoftnailServiceImpl==method:delete==errMsg: {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }
    }
}
